package com.dsz.repository;

import java.util.List;


import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import com.dsz.entity.Users;

public interface UsersRepository extends JpaRepository<Users, String>, JpaSpecificationExecutor<Users> {

	/**
	 * 根据登录名和密码查找
	 * @param loginname
	 * @param password
	 * @return
	 */
	Users findUsersByLoginnameAndPassword(String loginname, String password);

	
	/***
	 * 删除 单删
	 * 
	 * @param uid
	 * @return
	 */
	@Transactional
	@Modifying
	@Query(nativeQuery = true, value = ("delete from t_users where uid in (:uid)"))
	Integer getDelete(@Param("uid") String uid);

	/**
	 * 批量删除
	 * 
	 * @param uid
	 * @return
	 */
	@Transactional
	@Modifying
	@Query(nativeQuery = true, value = ("delete from t_users where uid in (:uid)"))
	Integer delStudent(@Param("uid") List<String> uid);

	/**
	 * 根据登录名查询是否有该用户
	 * 
	 * @param loginname
	 * @return
	 */
	Users findByLoginname(String loginname);

	/**
	 * 密码错误过多，锁定用户
	 * 
	 * @param uid
	 * @param lslockout
	 * @param locktime
	 * @return
	 */
	@Transactional
	@Modifying
	@Query(nativeQuery = true, value = ("update t_users set lslockout = :lslockout, locktime = :locktime where uid = :uid"))
	Integer updateLslockout(@Param("uid") String uid, @Param("lslockout") String lslockout,
			@Param("locktime") String locktime);

	/**
	 * 修改密码错误次数
	 * 
	 * @param uid
	 * @param psdwrongtime
	 * @return
	 */
	@Transactional
	@Modifying
	@Query(nativeQuery = true, value = ("update t_users set psdwrongtime = :psdwrongtime where uid = :uid"))
	Integer updatePsdwrongtime(@Param("uid") String uid, @Param("psdwrongtime") Integer psdwrongtime);
	
	//修改用户最后登录时间
		@Transactional
		@Modifying
		@Query(nativeQuery=true, value = ("update t_users set lasttime = :dateTime where uid = :uid"))
		Integer updateLasttime(@Param("uid")String uid, @Param("dateTime")String dateTime);

		//根据用户名和密码查询用户
		Users findByLoginnameAndPassword(String loginname, String password);


}
